<template>
  <div>
    <!-- 你可以在这里添加 Vue 模板代码 -->
  </div>
</template>

<script setup lang='ts'>
interface ProductInterface {
  id: number;
  name: string;
  price: number;
  category: Category;
  getDetails(): string;
}

enum Category {
  Electronics,
  Clothing,
  Food
}

class Product implements ProductInterface {
  private _id: number;
  public name: string;
  public price: number;
  public category: Category;
  private _stock: number;

  static taxRate: number = 0.2;

  constructor(id: number, name: string, price: number, category: Category, stock: number) {
    this._id = id;
    this.name = name;
    this.price = price;
    this.category = category;
    this._stock = stock;
  }

  public get id(): number {
    return this._id;
  }

  public get stock(): number {
    return this._stock;
  }

  public set stock(value: number) {
    if (value < 0) {
      throw new Error("Stock cannot be negative");
    }
    this._stock = value;
  }

  private isInStock(): boolean {
    return this._stock > 0;
  }

  public getDetails(): string {
    return `Product ID: ${this.id}, Name: ${this.name}, Price: $${this.price.toFixed(2)}, Category: ${Category[this.category]}, In Stock: ${this.isInStock()}`;
  }

  public static calculateTax(price: number): number {
    return price * this.taxRate;
  }
}

class ElectronicProduct extends Product {
  public warranty: number;

  constructor(id: number, name: string, price: number, category: Category, stock: number, warranty: number) {
    super(id, name, price, category, stock);
    this.warranty = warranty;
  }

  public getDetails(): string {
    return `${super.getDetails()}, Warranty: ${this.warranty} months`;
  }
}

interface Discountable<T> {
  applyDiscount(discount: T): number;
}

type PromotionFunction = (product: Product) => number;

class DiscountedProduct extends Product implements Discountable<number> {
  public applyDiscount(discount: number): number {
    const discountedPrice = this.price - discount;
    return discountedPrice >= 0 ? discountedPrice : 0;
  }
}

// 示例促销函数
const applyPromotion: PromotionFunction = (product) => {
  return product.price * 0.9;
};

const laptop = new ElectronicProduct(1, "Laptop", 1500, Category.Electronics, 10, 24);
console.log(laptop.getDetails());
console.log(`Tax on the price: $${Product.calculateTax(laptop.price).toFixed(2)}`);
console.log(`Discounted price: $${applyPromotion(laptop).toFixed(2)}`);
</script>

<style>
  /* 你可以在这里添加样式 */
</style>